package com.zhangxi.controller.admin;

import com.zhangxi.dto.SetMealDTO;
import com.zhangxi.dto.SetMealPageQueryDTO;
import com.zhangxi.result.PageResult;
import com.zhangxi.result.Result;
import com.zhangxi.service.SetMealService;
import com.zhangxi.vo.DishVo;
import com.zhangxi.vo.SetMealVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: SetMealController
 * Package: com.zhangxi.controller.admin
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理相关接口")
@Slf4j
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    /**
     * 分页查询套餐
     *
     * @param setMealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result page(SetMealPageQueryDTO setMealPageQueryDTO) {
        log.info("套餐分页查询:{}", setMealPageQueryDTO);
        PageResult pageResult = setMealService.page(setMealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     *
     * @param setMealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "SetMealCache",key = "#setMealDTO.categoryId")
    public Result insert(@RequestBody SetMealDTO setMealDTO) {
        log.info("新增套餐：{}", setMealDTO);
        setMealService.insertSetMeal(setMealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐信息")

    public Result getBySetMealId(@PathVariable Long id) {
        log.info("查询套餐id：{}", id);
        SetMealVo setMealVo = setMealService.getBySetMealId(id);
        return Result.success(setMealVo);
    }

    /**
     * 修改套餐信息
     *
     * @param setMealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐信息")
    @CacheEvict(cacheNames = "SetMealCache",allEntries = true)
    public Result updateSetMeal(@RequestBody SetMealDTO setMealDTO) {
        log.info("修改套餐：{}", setMealDTO);
        //修改操作
        setMealService.updateSetMeal(setMealDTO);
        return Result.success();
    }


    /**
     * 改变套餐启停状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("改变套餐启停状态")
    @CacheEvict(cacheNames = "SetMealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("修改套餐{}启停状态：{}", id, status);
        setMealService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id集合批量删除套餐")
    @CacheEvict(cacheNames = "SetMealCache",allEntries = true)
    public Result deleteBySetMealId(@RequestParam List<Long> ids) {
        log.info("删除套餐id：{}", ids);
        setMealService.deleteBySetMealId(ids);
        return Result.success();
    }

}
